package com.haruhinav.service.impl;

import com.haruhinav.Utils.TimeTool;
import com.haruhinav.entity.Hitoko;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.mapper.HitokoMapper;
import com.haruhinav.service.HitokoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HitokoServiceImpl implements HitokoService {


    @Resource
    private HitokoMapper hitokoMapper;
    @Autowired
    private TimeTool tTool;

    @Override
    public ResultVO insertHitoko(Hitoko hitoko) {
        ResultVO result = new ResultVO();
        Hitoko lastHitoko = hitokoMapper.findHitokoByLastTime(hitoko.getUserId());
        //String time = lastHitoko.getCreateTime();
        if(lastHitoko != null && !tTool.checkTimeAfterNow(lastHitoko.getCreateTime())){

            //不允许 今日已经添加过...
            result.setCode(-1);
            result.setMsg("今天已经写过了一言了！");
            result.setType("warning");
            return result;
        }

        int add_flag = hitokoMapper.addHitoko(hitoko);
        if(add_flag > 0 && hitoko.getId() != 0){
            result.setCode(0);
            result.setMsg("添加成功！");
            result.setData(hitoko);
        }else{
            result.setCode(-2);
            result.setMsg("添加失败！");
            result.setType("error");
        }
        return result;
    }

    @Override
    public Map<String,Object> findHitokoByUId(Integer uid) {
        Map<String,Object> map = new HashMap<>();
        ArrayList strs = new ArrayList();
        List<Hitoko> hitokos =hitokoMapper.findHitokoByUId(uid);

        //hitokos.forEach(h->h.setCreateTime(tTool.SubTime(h.getCreateTime())));

        //hitokos.forEach(h->strs.add(h.getContent()));

        for(Hitoko hitoko:hitokos){
            //        时间转格式
            hitoko.setCreateTime(tTool.SubTime(hitoko.getCreateTime()));
            strs.add(hitoko.getContent());
        }
        map.put("hitokos",hitokos);
        map.put("strs",strs);
       // System.out.println(">>>  "+hitokos);
        return map;
    }
//修改
    @Override
    public ResultVO updateHitokoById(Hitoko hitoko) {
        ResultVO result = new ResultVO();
        Hitoko findHitoko = hitokoMapper.findHitokoById(hitoko.getId());
        if(findHitoko != null && !tTool.checkTimeEqualsNow(findHitoko.getCreateTime())){

            //不允许 不是修改当天的一言...
            result.setCode(-1);
            result.setMsg("不能修改非当天添加的一言！");
            result.setType("warning");
            return result;
        }

        int update_flag = hitokoMapper.updateHitokoById(hitoko);
        if(update_flag > 0){
            result.setCode(0);
            result.setMsg("修改成功！");
            //result.setData(hitoko);
        }else{
            result.setCode(-2);
            result.setMsg("修改失败！");
            result.setType("error");
        }
        return result;
    }

    @Override
    public ResultVO checkDateEqualNow(int hid) {
        ResultVO result = new ResultVO();
        Hitoko hitoko = hitokoMapper.findHitokoById(hid);
        if(tTool.checkTimeEqualsNow(hitoko.getCreateTime())){
            result.setCode(0);
            result.setData(true);
            result.setMsg("OK");
            return result;
        }
        result.setCode(-1);
        result.setData(false);
        result.setMsg("ON");

        return result;
    }
}
